package com.apexedu.biz.service.impl;

import com.apexedu.biz.action.UserManagerAction;
import com.apexedu.framework.service.impl.AbstractBaseServiceSupport;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.Pager;
import com.apexedu.framework.util.SeqFactory;
import com.apexedu.identity.entity.TSysUser;
import com.apexedu.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service("userManagerService")
public class UserManagerServiceImpl extends AbstractBaseServiceSupport {
    
	@SuppressWarnings("unchecked")
	@Override
	public List<?> list(Object myaction, Pager pager) throws Exception {

        List<Object> params = new LinkedList<Object>();
        String sql = " from T_SYS_USER t where 1=1 ";
        /*if(project!=null){
            if(StringUtil.isNotBlank(project.getXmmc())){
                sql += " and t.xmmc like ?";
                params.add("%"+project.getXmmc()+"%");
            }
            if(StringUtil.isNotBlank(project.getSf())){
                sql += " and t.sf = ?";
                params.add(project.getSf());
            }*/

        long c = DBUtil.count("select count(*) as c " + sql, params.toArray());
        pager.setTotalRows(c);
        //hql.append(" order by ordernum desc");
        StringBuilder hql_column = new StringBuilder("select userid,userloginid,username,userpwd,(select departname from T_SYS_DEPART where departid=t.userdepart) as userdepart,state,(decode(t.usertype,'0','管理员',1,'业务管理')) as usertype,lastlogintime,logintimes,lastloginip ");
        hql_column.append(sql);
        List<TSysUser> list = DBUtil.queryPageBeanList(pager, hql_column.toString(), TSysUser.class, params.toArray());
        return list;
	}

	@Override
	public void load(Object myaction) throws Exception {
        UserManagerAction action = (UserManagerAction)myaction;
        String wid = action.getWid();
        TSysUser sysUser = getBaseDao().findById(TSysUser.class,wid);
        action.setSysUser(sysUser);
	}

	@Override
	public boolean remove(Object myaction) throws Exception {
        UserManagerAction action = (UserManagerAction) myaction;
        String ids = action.getWid();
        boolean deleteSuccess = getBaseDao().deleteAll("TSysUser", "wid", "=", ids);
        return deleteSuccess;
	}

	@Override
	public void saveOrUpdate(Object myaction) throws Exception {
        UserManagerAction action = (UserManagerAction)myaction;
        TSysUser sysUser = action.getSysUser();
        if(sysUser != null){
            if(StringUtil.isNotBlank(sysUser.getUserid())){
                baseDao.updateNotNull(sysUser);
            }else{
                sysUser.setUserid(SeqFactory.getNewSequenceAlone());
                baseDao.save(sysUser);
            }
        }
	}

}
